//
//  CDTabBar.m
//  CustomTabbar
//
//  Created by Dong Chen on 2017/9/1.
//  Copyright © 2017年 Dong Chen. All rights reserved.
//

#import "CDTabBar.h"
#import "HZHeadlinesView.h"

#import "RichTextEditViewController.h"
#import "CYPublishVideoViewController.h"
#import "CYPubilishQuestionViewController.h"

@interface CDTabBar()
@property (nonatomic, strong) UIButton *middleBtn;
@property (nonatomic, strong) UIButton *sendBtn;

@end

@implementation CDTabBar

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self addSubview:self.sendBtn];
    
    
    [self.sendBtn setImagePositionWithType:SSImagePositionTypeTop spacing:4];
    // 其他位置按钮
    NSUInteger count = self.subviews.count;
    for (NSUInteger i = 0 , j = 0; i < count; i++)
    {
        UIView *view = self.subviews[i];
        Class class = NSClassFromString(@"UITabBarButton");
        if ([view isKindOfClass:class])
        {
            view.width = self.width / 5.0;
            view.x = self.width * j / 5.0;
            j++;
            if (j == 2)
            {
                j++;
            }
        }
    }

}
// 发布
- (void)didClickPublishBtn:(UIButton*)sender {
    
    UIView *coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 300)];
    coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:@"42、发布-关闭发布"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    [coverView addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(coverView).with.mas_offset(-20);
//        make.centerX.mas_equalTo(kWindowW / 2);
        make.left.equalTo(coverView).with.mas_offset((kWindowW / 2) - 25);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.cornerRadius = 10;
    [coverView addSubview:backView];
    
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(btn.mas_top).with.mas_offset(-20);
        make.left.equalTo(coverView).with.mas_offset(15);
        make.top.equalTo(coverView).with.mas_offset(15);
        make.right.equalTo(coverView).with.mas_offset(-15);
    }];
    
    LPButton *titleBtn = [[LPButton alloc] init];
    titleBtn.style = LPButtonStyleRight;
    titleBtn.space = 4;
//    [titleBtn setImage:[UIImage imageNamed:@"jinbi"] forState:UIControlStateNormal];
//    titleBtn.imageView.contentMode = UIViewContentModeCenter;
    [titleBtn setTitle:@"" forState:UIControlStateNormal];
    [titleBtn setTitleColor:RGBOF(0x666666) forState:UIControlStateNormal];
    titleBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [backView addSubview:titleBtn];
    [titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView).with.mas_offset(5);
        make.top.equalTo(backView).with.mas_offset(5);
        make.right.equalTo(backView).with.mas_offset(-5);
        make.height.mas_equalTo(25);
    }];
    
    NSMutableArray *btns = [NSMutableArray arrayWithCapacity:0];
    NSArray *_titles = @[@"提问", @"发文章", @"拍视频"];
    NSArray *_images = @[@"41、发动态-提问", @"39、发动态-发文字", @"40、发动态-拍视频"];

    for (int i = 0; i < 3; i++) {
        LPButton *titleBtn = [[LPButton alloc] init];
        titleBtn.space = 10;
        [titleBtn setImage:[UIImage imageNamed:_images[i]] forState:UIControlStateNormal];
        [titleBtn setTitle:_titles[i] forState:UIControlStateNormal];
        [titleBtn setTitleColor:RGBOF(0x666666) forState:UIControlStateNormal];
        titleBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [backView addSubview:titleBtn];
        titleBtn.tag = i;
        [titleBtn addTarget:self action:@selector(fabuAction:) forControlEvents:UIControlEventTouchUpInside];
        [btns addObject:titleBtn];
    }
    
    // 实现masonry水平固定间隔方法
    // 实现masonry水平固定控件宽度方法
    [btns mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:(kWindowW - 30) / 3 leadSpacing:0 tailSpacing:0];
    
    // 设置array的垂直方向的约束
    [btns mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleBtn.mas_bottom).with.mas_offset(20);
        make.height.mas_equalTo(100);
    }];

    
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:coverView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

- (void)closeAction{
    [GKCover hide];
}

- (void)fabuAction:(UIButton *)sender{
    if (![YLUserInfo isLogIn]) {
        [self closeAction];
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    if (sender.tag == 0) {
        //提问
        CYPubilishQuestionViewController *question = [[CYPubilishQuestionViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:question];

    }else if (sender.tag == 1){
        //发文章
        RichTextEditViewController *fawenzhang = [[RichTextEditViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:fawenzhang];
    }else{
        //拍视频
        CYPublishVideoViewController *video = [[CYPublishVideoViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:video];
    }
    
    [GKCover hide];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.isHidden == NO)
    {
        CGPoint newP = [self convertPoint:point toView:self.middleBtn];
        if ( [self.middleBtn pointInside:newP withEvent:event])
        {
            return self.middleBtn;
        }else
        {
            return [super hitTest:point withEvent:event];
        }
    }
    else
    {
        return [super hitTest:point withEvent:event];
    }
}

- (UIButton *)sendBtn{
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc] init];
        [_sendBtn setImage:[UIImage imageNamed:@"tabbar_release"] forState:UIControlStateNormal];
        [_sendBtn setTitle:@"发头条" forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_sendBtn addTarget:self action:@selector(didClickPublishBtn:) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn.adjustsImageWhenHighlighted = NO;
        _sendBtn.size = CGSizeMake(self.bounds.size.width/5.0, 45);
        _sendBtn.centerX = self.width / 2;
        _sendBtn.centerY = 13;
        [_sendBtn setTitleColor:RGB(109, 109, 109) forState:UIControlStateNormal];
        [_sendBtn setTitleColor:RGB(244, 69, 73) forState:UIControlStateHighlighted];
        self.middleBtn = _sendBtn;
    }
    return _sendBtn;
}

@end
